﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wlmy.Ids4.Common
{
    public class CustomEqualityComparer<T> : IEqualityComparer<T> where T : class
    {
        private readonly Func<T, object> _expr;

        public CustomEqualityComparer(Func<T,object> expr)
        {
            _expr = expr;
        }
        public bool Equals([DisallowNull] T x, [DisallowNull] T y)
        {
            var first = _expr.Invoke(x);
            var sec = _expr.Invoke(y);
            if (first != null && first.Equals(sec))
                return true;
            else
                return false;
        }

        public int GetHashCode([DisallowNull] T obj)
        {
            return _expr.Invoke(obj).GetHashCode();
        }
    }
}
